<template>
  <div class="login-container">
    <el-card class="login-box">
      <h2 class="login-title">登录到您的账户</h2>
      <el-form @submit.prevent="handleSubmit" label-position="top">
        <!-- 用户名 -->
        <el-form-item label="用户名" class="compact-form-item">
          <el-input v-model="username" placeholder="请输入用户名"></el-input>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item label="密码" class="compact-form-item">
          <el-input
              v-model="password"
              type="password"
              placeholder="请输入密码"
          ></el-input>
        </el-form-item>

        <!-- 手机号码 -->
        <el-form-item label="手机号" class="compact-form-item">
          <el-input v-model="phoneNumber" placeholder="请输入手机号"></el-input>
        </el-form-item>

        <!-- 手机验证码 -->
        <el-form-item label="手机验证码" class="compact-form-item">
          <el-row :gutter="10">
            <el-col :span="14">
              <el-input v-model="smsCode" placeholder="请输入验证码"></el-input>
            </el-col>
            <el-col :span="10">
              <el-button @click="sendSmsCode" :disabled="smsTimer > 0">
                <span v-if="smsTimer === 0">发送验证码</span>
                <span v-else>{{ smsTimer }} 秒后重发</span>
              </el-button>
            </el-col>
          </el-row>
        </el-form-item>

        <!-- 图形验证码 -->
        <el-form-item label="图形验证码" class="compact-form-item">
          <el-row :gutter="10">
            <el-col :span="14">
              <el-input v-model="captchaCode" placeholder="请输入图形验证码"></el-input>
            </el-col>
            <el-col :span="10">
              <img :src="captchaUrl" alt="captcha" @click="refreshCaptcha" class="captcha-img" />
            </el-col>
          </el-row>
        </el-form-item>

        <!-- 错误消息 -->
        <div class="error-message" v-if="errorMessage">
          <el-alert
              title="登录失败"
              :description="errorMessage"
              type="error"
              show-icon
          />
        </div>

        <!-- 登录按钮 -->
        <el-form-item class="compact-form-item">
          <el-button type="primary" @click="handleSubmit" class="login-button">
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      username: '',
      password: '',
      phoneNumber: '',
      smsCode: '',
      captchaCode: '',
      captchaUrl: '',
      errorMessage: '',
      smsTimer: 0,
    };
  },
  mounted() {
    this.refreshCaptcha();
  },
  methods: {
    handleSubmit() {
      // 登录逻辑
    },
    sendSmsCode() {
      // 发送短信验证码逻辑
    },
    refreshCaptcha() {
      this.captchaUrl = `https://dummyimage.com/120x40/42b983/fff.png&text=${Math.random().toString(36).substring(2, 6)}`;
    },
  },
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 95%;
  background-color: #f0f2f5;
}

.login-box {
  width: 380px;
  padding: 20px 25px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  border-radius: 8px;
  background-color: white;
}

.login-title {
  text-align: center;
  margin-bottom: 20px;
  color: #333;
  font-size: 22px;
}

.compact-form-item {
  margin-bottom: 10px; /* 缩小表单项的上下间距 */
}

.el-input {
  margin-bottom: 0;
}

.login-button {
  width: 100%;
}

.error-message {
  margin-bottom: 10px;
}

.captcha-img {
  cursor: pointer;
  border: 1px solid #ddd;
  border-radius: 4px;
}
</style>
